package com.atguigu.rabbitmq.routing;

import com.atguigu.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author 杨林
 * @create 2022-10-07 14:22 星期五
 * description:这是一个路由模式的生产者
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取连接
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        //创建交换机
        channel.exchangeDeclare("direct_exchange", BuiltinExchangeType.DIRECT,true,false,false,null);

        //创建队列
        channel.queueDeclare("direct1_queue", true, false, false, null);
        channel.queueDeclare("direct2_queue", true, false, false, null);

        //队列1绑定error
        channel.queueBind("direct1_queue", "direct_exchange", "error");

        //队列2绑定error info warning
        channel.queueBind("direct2_queue", "direct_exchange", "error");
        channel.queueBind("direct2_queue", "direct_exchange", "info");
        channel.queueBind("direct2_queue", "direct_exchange", "warning");

        String msg1 = "这是error";
        String msg2 = "这是info";
        String msg3 = "这是warning";

        //发送消息
        channel.basicPublish("direct_exchange", "error", null, msg1.getBytes());
        channel.basicPublish("direct_exchange", "info", null, msg2.getBytes());
        channel.basicPublish("direct_exchange", "warning", null, msg3.getBytes());

        //关闭连接
        channel.close();
        connection.close();
    }
}
